import java.util.ArrayList;
import java.util.List;

class SearchMatrix1519 {
    /**
     * 题目核心 根据升序规律 方向查找
     */
    public boolean searchMatrix(int[][] matrix, int target) {
        int cx=0;
        int cy = matrix.length-1;
        int n = matrix.length;
        if(n==0)return false;
        int m = matrix[matrix.length-1].length;
        for(;;){
            if(cx<0||cx>=m)break;
            if(cy<0||cy>=n)break;
            System.out.printf("%d %d %d %d \n", cx, cy, m, n);
            int curNum = matrix[cy][cx];
            if(target>curNum){
                cx++;
            }else if(target<curNum){
                cy--;
            }else{
                System.out.println("find ->" + cx +"   "+ cy);
                return true;
            }
        }
        return false;
    }

    public static void main(String[] args) {
        // int[][] nums = { 
        //     { 1, 4, 7, 11, 15 }, 
        //     { 2, 5, 8, 12, 19 }, 
        //     { 3, 6, 9, 16, 22 }, 
        //     { 10, 13, 14, 17, 24 },
        //     { 18, 21, 23, 26, 30 } 
        // };
        int[][]nums = {{-1,3}};
        new SearchMatrix1519().searchMatrix(nums, 3);
    }

    public static void print(int[] array) {
        for (int i : array) {
            System.out.print(i + ",");
        }
        System.out.println();
        System.out.println("*********************");
    }
}